//
//  Helper.m
//  AmuckSlider
//
//  Created by AppsAmuck on 3/13/09.
//  Copyright 2009 AppsAmuck LLC. All rights reserved.
//

#import "SSStorage.h"
#import "GameSoundManager.h"


@implementation SSStorage

+ (void)sendEmailWithSubject:(NSString*)aSubject withBody:(NSString*)aBody{
	NSString *urlString = [NSString stringWithFormat: @"mailto:?subject=%@&body=%@", aSubject, aBody];
	urlString = [urlString stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
	NSURL* mailURL = [NSURL URLWithString: urlString]; 
	[[UIApplication sharedApplication] openURL: mailURL]; 
}

 
+ (NSString*)getUserValueForKey:(NSString*)aKey withDefault:(NSString*)aDefaultValue {
	NSString *result = [[NSUserDefaults standardUserDefaults] stringForKey: aKey];
	if (result == nil || [result isEqualToString:@""]) {
		return aDefaultValue;
	}
	return result;
}

+ (void)setUserValue:(NSString*)aValue forKey:(NSString*)aKey {	
	[[NSUserDefaults standardUserDefaults] setObject: aValue forKey: aKey];
	[[NSUserDefaults standardUserDefaults] synchronize];
}

//STATIC METHODS

+ (int) getCurrentLevelIndex
{
	CMLog(@"Game->getCurrentLevelIndex");
	return [[SSStorage getUserValueForKey:@"currentlevel" withDefault:@"1"] intValue];
}

+ (void) setCurrentLevelIndex:(int)aLevelIndex
{
	int highestLevelIndex = [[SSStorage getUserValueForKey:@"highestlevel" withDefault:@"1"] intValue]; 
	if (highestLevelIndex < aLevelIndex)
	{
		[SSStorage setUserValue:[NSString stringWithFormat:@"%d", aLevelIndex] forKey:@"highestlevel"];
	}
	[SSStorage setUserValue:[NSString stringWithFormat:@"%d", aLevelIndex] forKey:@"currentlevel"];
}

+ (int) getCurrentGameScore
{
	return [[SSStorage getUserValueForKey:@"currentGameScore" withDefault:@"0"] intValue];
}

+ (void) setCurrentGameScore:(int)aGameScore
{
	[SSStorage setUserValue:[NSString stringWithFormat:@"%d", aGameScore] forKey:@"currentGameScore"];
}

+ (int) getCurrentHealth
{
	return [[SSStorage getUserValueForKey:@"currentHealth" withDefault:@"100"] intValue];
}

+ (void) setCurrentHealth:(int)aHealth
{
	[SSStorage setUserValue:[NSString stringWithFormat:@"%d", aHealth] forKey:@"currentHealth"];
}

+ (int) getCurrentLivesLeft {
	return [[SSStorage getUserValueForKey:@"currentLivesLeft" withDefault:@"3"] intValue];
}

+ (void) setSound:(BOOL)sound
{
	CMLog(@"setSound: %d", sound);
	int i = 0;
	if (sound) {
		i = 1;
	}
	[SSStorage setUserValue:[NSString stringWithFormat:@"%d", i] forKey:@"sound"];
}

+ (BOOL) getSound {
	int i= [[SSStorage getUserValueForKey:@"sound" withDefault:@"1"] intValue];
	if (i == 1) {
		return YES;
	}else {
		return NO;
	}

}

+ (void) setMusic:(BOOL)music
{
	int i = 0;
	if (music) {
		i = 1;
	}
	[SSStorage setUserValue:[NSString stringWithFormat:@"%d", i] forKey:@"music"];
}

+ (BOOL) getMusic {
	int i= [[SSStorage getUserValueForKey:@"music" withDefault:@"1"] intValue];
	if (i == 1) {
		return YES;
	}else {
		return NO;
	}
	
}

+ (void) setCurrentLivesLeft:(int)aLivesLeft {
	[SSStorage setUserValue:[NSString stringWithFormat:@"%d", aLivesLeft] forKey:@"currentLivesLeft"];
}

+ (int) getDefaultLives {
	return [[SSStorage getUserValueForKey:@"defaultLivesLeft" withDefault:@"0"] intValue];
}

+ (void) setDefaultLives:(int)aLivesLeft
{
	CMLog(@"Game->setDefaultLivesLeft: %i", aLivesLeft);
	[SSStorage setUserValue:[NSString stringWithFormat:@"%d", aLivesLeft] forKey:@"defaultLivesLeft"];
}

@end
